#include"TableWidget.h"
#include<QtDebug>
TableWidget::TableWidget(QWidget *parent):QTableWidget(parent)
{
    PopMenu = NULL;
}
TableWidget::~TableWidget()
{}

void TableWidget::setPopMenu( QMenu *menu)
{
    PopMenu = menu;
}

void TableWidget::mousePressEvent(QMouseEvent *e)
{

    if(PopMenu == NULL)
    {
        QMessageBox prompt;
        prompt.setText("菜单指针为空");
        prompt.exec();
        return;
    }
    if( e->button() == Qt::LeftButton)
    {
            //do something ??
        pos= e->pos();
    }
    else if( e->button() == Qt::RightButton)
    {
        QPoint p = e->pos();
        p = mapToGlobal(p);
        PopMenu->popup(p);
        qDebug()<<"rightbutton"<<p;
    }
}

void TableWidget::setTableType(int type)
{
    this->type = type;
    PopMenu = new QMenu(this);
    if(this->type == TODAYTABLE)
    {
        Continue = new QAction(tr("继续"),this);
        Stop = new QAction(tr("停止"),this);
        connect(Continue,SIGNAL(triggered()),this,SLOT(CGetFileName()));
        connect(Stop,SIGNAL(triggered()),this,SLOT(SGetFileName()));
        PopMenu->addAction(Continue);
        PopMenu->addAction(Stop);
    }
    OpenDir = new QAction(tr("打开目录"),this);
    PopMenu->addAction(OpenDir);
    connect(OpenDir,SIGNAL(triggered()),this,SLOT(GetPath()));
}

void TableWidget::CGetFileName(void)
{
    QTableWidgetItem* item = this->itemAt(pos);
    if(item == NULL)
        return ;
    int row = this->row(item);
    QString filename = this->item(row,FN)->text();
    QString dir = this->item(row,DIR)->text();
    if(filename.at(filename.size()-1).toAscii() =='/')
        emit ContinueDownload(dir+filename);
    else
        emit ContinueDownload(dir+"/"+filename);

}

void TableWidget::SGetFileName(void)
{
   QTableWidgetItem* item = this->itemAt(pos);
   if(item == NULL)
       return ;
   int row = this->row(item);
   QString filename = this->item(row,FN)->text();
   QString dir = this->item(row,DIR)->text();
   if(filename.at(filename.size()-1).toAscii() =='/')
       emit StopDownload(dir+filename);
   else
       emit StopDownload(dir+"/"+filename);

}

void TableWidget::GetPath()
{
    QTableWidgetItem* item = this->itemAt(pos);
    if(item == NULL)
        return ;
    int row = this->row(item);
    QString dir = this->item(row,DIR)->text();
}
